package repository

import (
	"common/query"
	"gorm.io/gorm"
)

type Repository struct {
	DB *gorm.DB
}

func (r *Repository) First(t interface{}) {
	r.DB.First(t)
}

func (r *Repository) FirstFilter(t any, filters *query.Filters) error {
	conds, _ := query.FiltersToConds(filters)
	w, err := query.CondsToWriter(conds)
	if err != nil {
		return err
	}
	r.DB.Where(w.String(), w.Args()...).First(t)
	return nil
}
